/*
 * Related Posts - http://salman-w.blogspot.com/2012/09/blogger-related-posts.html
 * Part of Blogger Utilities - http://code.google.com/p/blogger-utilities/
 * Copyright 2012-2014 Salman Arshad
 * This code is released under MIT license
 */
(function() {
	var config = {
		maxTagsToQuery: 5,
		maxResultsPerTag: 10,
		maxResultsToDisplay: 5
	}, postLink, postTags = [], pendingRequests = 0, relatedPosts = [], a, i, script;
	for (a = document.getElementsByTagName("link"), i = 0; i < a.length; i++) {
		if (a[i].rel === "canonical") {
			postLink = a[i].href;
			break;
		}
	}
	if (/\x2F\d{4}\x2F\d{2}\x2F/.test(postLink) === false) {
		return;
	}
	for (a = document.getElementsByTagName("a"), i = 0; i < a.length; i++) {
		if (a[i].rel === "tag") {
			postTags.push(a[i].textContent || a[i].innerText);
		}
	}
	window.bu_RelatedPosts_track = function(a) {
		if (typeof _gaq === "object" || typeof ga === "function") {
			if (typeof _gaq === "object") {
				_gaq.push(["_trackEvent", "BU - Related Posts", "Related Post Clicked", a.href]);
			}
			if (typeof ga === "function") {
				ga("send", "event", "BU - Related Posts", "Related Post Clicked", a.href);
			}
			setTimeout(function() {
				location.href = a.href;
			}, 500);
			return false;
		}
	};
	window.bu_RelatedPosts_callback = function(data) {
		var tempLink, tempPost, b, j, div, html;
		for (a = data.feed.entry, i = 0; i < a.length; i++) {
			for (b = a[i].link, j = 0; j < b.length; j++) {
				if (b[j].rel === "alternate") {
					tempLink = b[j].href;
					break;
				}
			}
			if (tempLink === postLink) {
				continue;
			}
			for (b = relatedPosts, j = 0; j < b.length; j++) {
				if (b[j].link === tempLink) {
					b[j].count++;
					break;
				}
			}
			if (j === b.length) {
				tempPost = {
					link: tempLink,
					title: a[i].title.$t,
					tags: [],
					count: 1
				};
				for (b = a[i].category, j = 0; j < b.length; j++) {
					tempPost.tags.push(b[j].term);
				}
				if (a[i].media$thumbnail) {
					tempPost.icon = {
						src: a[i].media$thumbnail.url,
						width: a[i].media$thumbnail.width,
						height: a[i].media$thumbnail.height
					};
				}
				relatedPosts.push(tempPost);
			}
		}
		pendingRequests--;
		if (pendingRequests) {
			return;
		}
		relatedPosts.sort(function(a, b) {
			return b.count - a.count;
		});
		div = document.createElement("div");
		div.id = "bu-related-posts";
     html = "<p><h4>相關文章：</h4></p>";
		html += "<ul>";
		for (a = relatedPosts.slice(0, config.maxResultsToDisplay), i = 0; i < a.length; i++) {
			html += "<li><a href='" + a[i].link + "' onclick='return bu_RelatedPosts_track(this);' title='" + a[i].count + " common tag(s)'><span style='" + (a[i].icon ? "background: url(" + a[i].icon.src + ") no-repeat center center;" : "") + "'></span>" + a[i].title.replace(/&/g, "&amp;").replace(/\x22/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</a> <small>" + a[i].tags.join(", ") + "</small></li>";
		}
		html += "</ul>";
		div.innerHTML = html;
		for (a = document.getElementsByTagName("div"), i = 0; i < a.length; i++) {
			if (a[i].className && (" " + a[i].className + " ").indexOf(" post ") !== -1) {
				a[i].appendChild(div);
				break;
			}
		}
	};
	for (a = postTags.slice(0, config.maxTagsToQuery), i = 0; i < a.length; i++) {
		script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "/feeds/posts/summary?alt=json&callback=bu_RelatedPosts_callback&max-results=" + config.maxResultsPerTag + "&category=" + encodeURIComponent(a[i]);
		document.getElementsByTagName("head")[0].appendChild(script);
		pendingRequests++;
	}
})();
